package cn.tedu.jsd2207csmall.passport.ex.handler;

import cn.tedu.jsd2207csmall.passport.ex.ServiceException;
import cn.tedu.jsd2207csmall.passport.web.JsonResult;
import cn.tedu.jsd2207csmall.passport.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;
import java.util.StringJoiner;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    public GlobalExceptionHandler() {
        log.debug("创建全局异常处理器对象:GlobalExceptionHandler");
    }

    @ExceptionHandler({
            InternalAuthenticationServiceException.class,
            BadCredentialsException.class
    })
    public JsonResult<Void>  handleAuthenticationException(AuthenticationException e){
        log.info("捕获到AuthenticationException异常");
        log.info("异常类型:{}",e.getClass().getName());
        log.info("异常消息:{}",e.getMessage());
        String message="用户名或密码错误";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED,message);
    }

    @ExceptionHandler
    public JsonResult<Void> handleDisabledException(DisabledException e){
        log.info("捕获到:DisabledException");
        log.info("登陆失败此账号已经禁用");
        String message="登陆失败此账号已经禁用";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED_DISABLED,message);
    }

    @ExceptionHandler
    public String handleThrowable(Throwable e) {
        String message = "你有异常没有处理，请根据服务器端控制台的信息，补充对此类异常的处理！！！";
        e.printStackTrace();
        return message;
    }

    @ExceptionHandler
    public JsonResult<Void> ex(ServiceException e){
        log.debug("处理请求的方法抛出了ServiceException异常,将会统一处理");

        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public JsonResult<Void> accessDeniedException(AccessDeniedException e){
        log.debug("处理请求的方法抛出了AccessDeniedException异常,将会统一处理");
        String message="访问失败，当前登录的用户不具有此操作权限！";
        return JsonResult.fail(ServiceCode.ERR_FORBIDDEN,message);
    }

    @ExceptionHandler
    public JsonResult<Void> handleBindException(BindException e){
        log.debug("开始处理BindException");

        String defaultMessage = e.getFieldError().getDefaultMessage();

       /* StringJoiner stringJoiner= new StringJoiner(",","请求参数格式错误:","!!!!");
        List<FieldError> fieldErrors = e.getFieldErrors();
        for(FieldError fieldError:fieldErrors){
            String defaultMessage = fieldError.getDefaultMessage();
            stringJoiner.add(defaultMessage);
        }*/

        return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST,defaultMessage);
    }

    @ExceptionHandler
    public JsonResult handlerConstraintViolationException(ConstraintViolationException e){
        log.debug("开始处理:ConstraintViolationException");
        StringJoiner joiner=new StringJoiner(",");
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        for (ConstraintViolation<?> constraintViolation : constraintViolations) {
            joiner.add(constraintViolation.getMessage());
        }
        return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST,joiner.toString());
    }

}
